﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuaLuuNiem.Models;
using PagedList;
using System.IO;
using System.Data;
namespace QuaLuuNiem.Controllers
{
    public class AdminController : Controller
    {
        private DOAN_QUA_LUU_NIEMEntities db = new DOAN_QUA_LUU_NIEMEntities();
        //
        // GET: /Admin/

        public ActionResult danh_sach_san_pham(string currentFilter, string key, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                key = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = key;
            var sanpham = from s in db.SANPHAMs select s;
            if (!String.IsNullOrEmpty(key))
            {
                sanpham = sanpham.Where(s => s.TenSP.ToUpper().Contains(key.ToUpper()));
            }
            sanpham = sanpham.OrderBy(s => s.MaSP);
            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(sanpham.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult ThemSanPham()
        {
            ViewBag.MaLoaiSP = new SelectList(db.LOAISANPHAMs, "MaLoaiSP", "TenLoai");
            ViewBag.TrangThai = new SelectList(db.TRANGTHAISPs, "MaTrangThai", "TenTrangThai");
            ViewBag.MaMenu = new SelectList(db.MENURIGHTs, "MaMenu", "TenMenu");
            return View();
        }
        [HttpPost]
        public ActionResult ThemSanPham(SANPHAM sanpham,HttpPostedFileBase Hinh)
        {
            ViewBag.MaLoaiSP = new SelectList(db.LOAISANPHAMs, "MaLoaiSP", "TenLoai");
            ViewBag.TrangThai = new SelectList(db.TRANGTHAISPs, "MaTrangThai", "TenTrangThai");
            ViewBag.MaMenu = new SelectList(db.MENURIGHTs, "MaMenu", "TenMenu");
            if (ModelState.IsValid)
            {
                if (Hinh != null && Hinh.ContentLength > 0)
                {
                    string tenHinh = DateTime.Now.ToFileTime().ToString()+Hinh.FileName;
                    string filepath = Path.Combine(Server.MapPath("~/Content/images/san_pham"), Path.GetFileName(tenHinh));
                    Hinh.SaveAs(filepath);
                    sanpham.Hinh = tenHinh;
                }
                db.SANPHAMs.AddObject(sanpham);
                db.SaveChanges();
                return RedirectToAction("danh_sach_san_pham");
            }
            return View(sanpham);
        }
        public ActionResult CapNhatSanPham(int id)
        {
            ViewBag.MaLoaiSP = new SelectList(db.LOAISANPHAMs, "MaLoaiSP", "TenLoai");
            ViewBag.TrangThai = new SelectList(db.TRANGTHAISPs, "MaTrangThai", "TenTrangThai");
            ViewBag.MaMenu = new SelectList(db.MENURIGHTs, "MaMenu", "TenMenu");
            SANPHAM sanpham = db.SANPHAMs.Single(s => s.MaSP == id);
            return View(sanpham);
        }
        [HttpPost]
        public ActionResult CapNhatSanPham(SANPHAM sanpham)
        {
            ViewBag.MaLoaiSP = new SelectList(db.LOAISANPHAMs, "MaLoaiSP", "TenLoai");
            ViewBag.TrangThai = new SelectList(db.TRANGTHAISPs, "MaTrangThai", "TenTrangThai");
            ViewBag.MaMenu = new SelectList(db.MENURIGHTs, "MaMenu", "TenMenu");
            if (ModelState.IsValid)
            {
                db.SANPHAMs.Attach(sanpham);
                db.ObjectStateManager.ChangeObjectState(sanpham, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("danh_sach_san_pham");
            }
            return View(sanpham);
        }
        public ActionResult XoaSanPham(int id)
        {
            SANPHAM sanpham = db.SANPHAMs.Single(s => s.MaSP == id);
            return View(sanpham);
        }
        [HttpPost, ActionName("XoaSanPham")]
        public ActionResult XoaSanPhamConfirm(int id)
        {
            SANPHAM sanpham = db.SANPHAMs.Single(s => s.MaSP == id);
            string path = "~/Content/images/san_pham/" + sanpham.Hinh;
            if (System.IO.File.Exists(Server.MapPath( path)))
            {
                System.IO.File.Delete(Server.MapPath(path));
            }
            db.SANPHAMs.DeleteObject(sanpham);
            db.SaveChanges();
            return RedirectToAction("danh_sach_san_pham");
        }

        
        public ActionResult danh_sach_bai_viet(string currentFilter, string key, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                key = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = key;
            var baiviet = from s in db.BAIVIETs select s;
            if (!String.IsNullOrEmpty(key))
            {
                baiviet = baiviet.Where(s => s.TenBaiViet.ToUpper().Contains(key.ToUpper()));
            }
            baiviet = baiviet.OrderBy(s => s.MaBaiViet);
            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(baiviet.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult ThemBaiViet()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ThemBaiViet(BAIVIET baiviet,HttpPostedFileBase Hinh)
        {
            if (ModelState.IsValid)
            {
                baiviet.Hinh = "Chưa có hình ảnh cho bài viết này";
                if (Hinh != null)
                {
                    string tenHinh = DateTime.Now.ToFileTime().ToString() +"_bai_viet_"+ Hinh.FileName;
                    string filepath = Path.Combine(Server.MapPath("~/Content/images/bai_viet"), Path.GetFileName(tenHinh));
                    Hinh.SaveAs(filepath);
                    baiviet.Hinh = tenHinh;
                }
                    
                db.BAIVIETs.AddObject(baiviet);
                baiviet.NgayViet = DateTime.Now;
                db.SaveChanges();
                return RedirectToAction("danh_sach_bai_viet");
            }
            return View(baiviet);
        }
        public ActionResult XoaBaiViet(int id)
        {
            BAIVIET baiviet = db.BAIVIETs.Single(s => s.MaBaiViet == id);
            return View(baiviet);
        }
        [HttpPost, ActionName("XoaBaiViet")]
        public ActionResult XoaBaiVietConfirm(int id)
        {
            BAIVIET baiviet = db.BAIVIETs.Single(s => s.MaBaiViet == id);
            string path = "~/Content/images/bai_viet/" + baiviet.Hinh;
            if (System.IO.File.Exists(Server.MapPath(path)))
            {
                System.IO.File.Delete(Server.MapPath(path));
            }
            db.BAIVIETs.DeleteObject(baiviet);
            db.SaveChanges();
            return RedirectToAction("danh_sach_bai_viet");
        }

        public ActionResult CapNhatBaiViet(int id)
        {
            BAIVIET baiviet = db.BAIVIETs.Single(s => s.MaBaiViet == id);
            return View(baiviet);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CapNhatBaiViet(BAIVIET baiviet)
        {
            if (ModelState.IsValid)
            {
                db.BAIVIETs.Attach(baiviet);
                db.ObjectStateManager.ChangeObjectState(baiviet, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("danh_sach_bai_viet");
            }
            return View(baiviet);
        }

        public ActionResult danh_sach_menu()
        {
            return View(db.MENURIGHTs.ToList());
        }
        public ActionResult ThemMenu()
        {
            return View();
        }
        [HttpPost]
        public ActionResult ThemMenu(MENURIGHT menu)
        {
            if (ModelState.IsValid)
            {
                db.MENURIGHTs.AddObject(menu);
                db.SaveChanges();
                return RedirectToAction("danh_sach_menu");
            }
            return View(menu);
        }
        public ActionResult CapNhatMenu(int id)
        {
            MENURIGHT mn = db.MENURIGHTs.Single(s => s.MaMenu == id);
            return View(mn);
        }
        [HttpPost]
        public ActionResult CapNhatMenu(MENURIGHT menu)
        {
            if (ModelState.IsValid)
            {
                db.MENURIGHTs.Attach(menu);
                db.ObjectStateManager.ChangeObjectState(menu, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("danh_sach_menu");
            }
            return View(menu);
        }
        public ActionResult XoaMenu(int id)
        {
            MENURIGHT menu = db.MENURIGHTs.Single(s => s.MaMenu == id);
            return View(menu);
        }
        [HttpPost,ActionName("XoaMenu")]
        public ActionResult XoaMenuConfirm(int id)
        {
            MENURIGHT menu = db.MENURIGHTs.Single(s => s.MaMenu == id);
            try
            {
                db.DeleteObject(menu);
                db.SaveChanges();
                return RedirectToAction("danh_sach_menu");
            }
            catch (Exception)
            {
                ViewBag.Loi = "Không thể xóa được> Vì menu này đâng được hoạt động";
                return RedirectToAction("XoaMenu");
            }
        }

        public ActionResult danh_sach_tai_khoan(string currentFilter, string key, int? page)
        {
            if (Request.HttpMethod == "GET")
            {
                key = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = key;
            var khachhang = from s in db.KHACHHANGs select s;
            if (!String.IsNullOrEmpty(key))
            {
                khachhang = khachhang.Where(s => s.HoTen.ToUpper().Contains(key.ToUpper()));
            }
            khachhang = khachhang.OrderBy(s => s.MaKH);
            int pageSize = 8;
            int pageNumber = (page ?? 1);
            return View(khachhang.ToPagedList(pageNumber, pageSize));
        }
        public ActionResult XoaTaiKhoan(int id)
        {
            KHACHHANG kh = db.KHACHHANGs.Single(s => s.MaKH == id);
            return View(kh);
        }
        [HttpPost, ActionName("XoaTaiKhoan")]
        public ActionResult XoaTaiKhoanConfirm(int id)
        {
            KHACHHANG kh = db.KHACHHANGs.Single(s => s.MaKH == id);
            try
            {
                db.DeleteObject(kh);
                db.SaveChanges();
                return RedirectToAction("danh_sach_tai_khoan");
            }
            catch (Exception)
            {
                return RedirectToAction("XoaTaiKhoan");
            }

        }
    }
}
